package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.PropertyMapper;
import com.parkingspace.entity.Property;
import com.parkingspace.service.IPropertyService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 物业表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements IPropertyService {

	@Resource
	private RedisTemplate<String, Property> redisTemplate;

	@Resource
	private PropertyMapper propertyMapper;

	@Override
	public List<Property> findAll() throws Exception {
		//查询出所有的物业公司先去redis中查找如果没有的话就去查询数据库
		List<Property> wuye = redisTemplate.opsForList().range("wuye", 0, -1);
		if(wuye.size() == 0) {
			synchronized (ParkingInformationServiceImpl.class) {
				if (wuye.size() == 0) {
					List<Property> properties = propertyMapper.selectList(null);
					redisTemplate.opsForList().rightPushAll("wuye", properties);
					redisTemplate.expire("wuye",5, TimeUnit.MINUTES);
					wuye = redisTemplate.opsForList().range("wuye", 0, -1);
				}
			}
		}
		return wuye;
	}
}
